<template>
    <view class="downLoad">
        <view class="downList">
            <view class="list " v-for="(item,index) in dataList" :key="index">
                <view class="listTop flex flex-align flex-bt">
                    <view class="time">{{ item.create_at }}导出记录</view>
                    <view class="right flex flex-align">
                        <button class="blue-linear">{{item.progress}}</button>
                       
                    </view>
                </view>
                <view class="listCenter">
                    <view class="webzi">链接地址</view>
                    <view class="address flex flex-align flex-bt">
                        <view class="flex-1">生成中...</view>
                        <image
                            src="/static/teamcenter/copy.png"
                            mode="scaleToFill"
                            @click="copyText(item.xurl)"
                        />
                    </view>
                </view>
            </view>
            <view  v-if="list.length !== 0" class="list " v-for="(item,index) in list" :key="index">
                <view class="listTop flex flex-align flex-bt">
                    <view class="time">{{ item.create_at }}导出记录</view>
                    <view class="right flex flex-align">
                        <button @click="exportData(item.xurl)" class="blue-linear">导出</button>
                        <image @click="delExport(item.id)" src="/static/teamcenter/del.png" mode="scaleToFill"  />
                    </view>
                </view>
                <view class="listCenter">
                    <view class="webzi">链接地址</view>
                    <view class="address flex flex-align flex-bt">
                        <view class="flex-1">{{ item.xurl }}</view>
                        <image
                            src="/static/teamcenter/copy.png"
                            mode="scaleToFill"
                            @click="copyText(item.xurl)"
                        />
                    </view>
                </view>
            </view>
            <view v-if="dataList.length === 0&& list.length === 0" class="noData" style="padding-top: 10vh;">
                <image src="/static/nodata/nodata02.png" />
                <view class="noDataText">暂无数据</view>
            </view> 
        </view>
    </view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import API_CENTER from '@/api/teamcenter'
import { onLoad, onReachBottom } from '@dcloudio/uni-app';
import { useInfiniteLoad } from '@/hooks/useInfiniteScroll';
import { copyText, goUrl, toast } from '@/utils'
onLoad(() => {
    getList()
    loadMore()
})

const { list, isLoading, hasMore, loadMore, reset, params } = useInfiniteLoad(API_CENTER.getExportDataList, {
    initialParams: {},
    transformResponse: (response: any) => {
        return response.list || [];
    }
});

// 存储定时器 ID
let intervalId: NodeJS.Timeout | null = null;
const dataList = ref<any>([])
const getList = async () => {
    const res = await API_CENTER.getExportDataList({ status: 1 })
    dataList.value = res.list
    if (res.total !== 0) {
        if (!intervalId) {
            intervalId = setInterval(getList, 3000);
        }
    } else {
        // 如果 total 为 0，清除定时器
        if (intervalId) {
            clearInterval(intervalId);
            intervalId = null;
            reset()
            loadMore()
        }
    }
}

const delExport = async (id: number) => {
    await API_CENTER.delExportData({id:id})
    toast('删除成功')
    setTimeout(() => {
        reset()
        loadMore()
    }, 1000);
}

const exportData = async (url: string) => {
    // #ifdef MP-WEIXIN
    uni.downloadFile({
        url: url,
        success: (res) => {
            if (res.statusCode === 200) {
                uni.getFileSystemManager().saveFile({
                    tempFilePath: res.tempFilePath,
                    success: (res) => {
                        setTimeout(()=>{
                            uni.openDocument({  // 打开文档的API
                            filePath:res.savedFilePath,  // 文件地址
                            fileType:'xlsx' //打开的文件类型
                            })
                        },2000)
                    
                    },
                    fail: (err) => {
                        uni.showToast({
                        title: '保存失败',
                        icon: 'none',
                        duration: 2000
                        })
                    }
                })
            }
        }
    })
    // #endif
    // #ifndef MP-WEIXIN
    goUrl('/pagesA/yulanDl/yulanDl?url='+url,true)
    // #endif
}

onReachBottom(() => {
    // 还有更多数据且不在加载中时，加载更多
    if (hasMore.value && !isLoading.value) {
        loadMore()
    }
})
</script>
<style scoped lang="scss">
.downLoad {
    padding: 30rpx;
    .list{
        background: #fff;border-radius: 30rpx;padding:0 30rpx 30rpx;margin-bottom:20rpx;
        .listTop{
            padding: 30rpx 0;
            font-size: 28rpx;
            color: #333;
            font-weight: bold;
            button{margin: 0;padding: 0rpx 40rpx;color: #fff;font-size: 24rpx;border-radius: 40rpx;}
            image{
                width: 40rpx;height: 40rpx;margin-left: 10rpx;
            }
        }
        .listCenter{background: #F5F6FA;padding:20rpx;border-radius: 20rpx;
            .webzi{
                font-size: 24rpx;
                color: #999;
                font-weight: bold;margin-bottom: 6rpx;
            }
            .address{
                .flex-1{
                    flex: 1;word-break: break-all;  color: #323233;
                }
            }
            image{
                width: 40rpx;height: 40rpx;margin-left: 10rpx;
            }
        }
    }
}
</style>
